package TestResult;

import jiazhong.Dao.IUsersDao;
import jiazhong.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import tools.initLogRecord;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

//接口写sql
public class Test2 {
    private static InputStream in;
    private static SqlSessionFactory factory;
    private static SqlSession sqlSession;
    private static IUsersDao mapperDao;

    public static void init() throws IOException {
        //1.读取加载核心配置文件
        in = Resources.getResourceAsStream("config/mybatis-config2.xml");
        //2.构造会话工厂
        factory = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        sqlSession = factory.openSession();
        mapperDao = sqlSession.getMapper(IUsersDao.class);
    }

    @Test
    public void testFindAll() throws IOException {
        //日志管理
        initLogRecord.initLog();
        init();
        List<User> all = mapperDao.findAll();
        for (User o : all) {
            System.out.println(o);
        }
    }

    @Test
    public void insert() throws IOException {
        //日志管理
        initLogRecord.initLog();
        init();

        User user = new User();
        user.setId(7);
        user.setName("11");
        user.setPassword("123456");
        user.setNickname("KKK");
        mapperDao.saveUser(user);
        sqlSession.commit();
        sqlSession.close();
        in.close();
    }


    @Test
    public void update() throws IOException {
        //日志管理
        initLogRecord.initLog();
        init();

        User user = new User();
        user.setId(7);
        user.setName("88");
        user.setPassword("123456");
        user.setNickname("4545");
        mapperDao.updateUser(user);
        sqlSession.commit();
        sqlSession.close();
        in.close();
    }

    @Test
    public void deleteUser() throws IOException {
        //日志管理
        initLogRecord.initLog();
        init();
        User user = new User();
        user.setId(7);

        mapperDao.deleteUser(user);

        sqlSession.commit();
        sqlSession.close();
        in.close();
    }


    @Test
    public void testFindone() throws IOException {
        //日志管理
        initLogRecord.initLog();
        init();
        User user = new User();
        user.setId(5);

        List<User> all = mapperDao.findone(user);
        for (User o : all) {
            System.out.println(o);
        }
    }


    @Test
    public void testFindlike() throws IOException {
        //日志管理
        initLogRecord.initLog();
        init();

        String password = "12";
        List<User> all = mapperDao.findlike(password);
        for (User o : all) {
            System.out.println(o);
        }
    }

    @Test
    public void testFindtotal() throws IOException {
        //日志管理
        initLogRecord.initLog();
        init();
        int i = mapperDao.findtotal();
        System.out.println(i);
    }

}

